package com.haidechizi.fastadmin.modules.code.controller;

import com.haidechizi.fastadmin.common.page.PageDto;
import com.haidechizi.fastadmin.common.utils.PageInfo;
import com.haidechizi.fastadmin.common.utils.Response;
import com.haidechizi.fastadmin.common.validator.ValidatorUtils;
import com.haidechizi.fastadmin.common.base.AbstractController;
import com.haidechizi.fastadmin.modules.code.entity.DynamicCodeConfig;
import com.haidechizi.fastadmin.modules.code.service.DynamicCodeConfigService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.Arrays;


/**
 * 动态资源Controller
 * @author wangdejun
 * @version 2020-11-05
 */
@RestController
@RequestMapping(value = "code/dynamicCodeConfig")
public class DynamicCodeConfigController extends AbstractController {

	@Autowired
	private DynamicCodeConfigService dynamicCodeConfigService;


	/**
     * 动态资源列表
     */
	@RequiresPermissions("code:dynamicCodeConfig:list")
	@RequestMapping(value = "list")
	public Response list(PageDto pageDto, DynamicCodeConfig dynamicCodeConfig) {
		PageInfo page = dynamicCodeConfigService.findPage(pageDto, dynamicCodeConfig);
        return Response.ok().put("page", page);
	}

	/**
     * 动态资源信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("code:dynamicCodeConfig:info")
    public Response info(@PathVariable("id") Long id) {
        DynamicCodeConfig dynamicCodeConfig = dynamicCodeConfigService.selectById(id);
        return Response.ok().put("dynamicCodeConfig", dynamicCodeConfig);
    }

	/**
	* 动态资源保存
	*/
	@RequiresPermissions("code:dynamicCodeConfig:save")
	@RequestMapping(value = "save")
	public Response save(@RequestBody DynamicCodeConfig dynamicCodeConfig) {
        //校验类型
        ValidatorUtils.validateEntity(dynamicCodeConfig);
        dynamicCodeConfigService.insert(dynamicCodeConfig);
        return Response.ok();
    }

    /**
     * 动态资源修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("code:dynamicCodeConfig:update")
    public Response update(@RequestBody DynamicCodeConfig dynamicCodeConfig) {
        //校验类型
        ValidatorUtils.validateEntity(dynamicCodeConfig);
        dynamicCodeConfigService.updateById(dynamicCodeConfig);
        return Response.ok();
    }


	/**
     * 动态资源删除
     */
	@RequiresPermissions("code:dynamicCodeConfig:delete")
	@RequestMapping(value = "delete")
	public Response delete(@RequestBody Long[] ids) {
		dynamicCodeConfigService.deleteBatchIds(Arrays.asList(ids));
        return Response.ok();
	}
}